﻿Public Class frmLogin
#Region "Propertys"

    Private intIntentos As Integer = 0

    Public Property UsuarioNombre As String
        Get
            Return Me.txtUsuario.Text
        End Get
        Set(ByVal value As String)
            Me.txtUsuario.Text = value
        End Set
    End Property

    Public Property UsuarioPass As String
        Get
            Return Me.txtpass.Text
        End Get
        Set(ByVal value As String)
            Me.txtpass.Text = value
        End Set
    End Property
    Public ReadOnly Property UsuarioPassEncrypt As String
        Get
            Return Vichama.Components.Cifrado.EncriptarTexto(Me.txtpass.Text, Vichama.Configurations.Aplication.Passwords.PasswordIni)
        End Get

    End Property


#End Region


    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Close()
    End Sub

    Private Sub btnValidar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidar.Click
        AEntrar()
    End Sub

    Private Function Validar() As Boolean

        Dim blnValue As Boolean = True
        '/*validar usuario*/
        If Me.UsuarioNombre = "" Then
            MessageBox.Show("Ingrese el nombre del usuario", _
            Vichama.Configurations.Aplication.MessageBox.MensajeTitulo, _
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.txtUsuario.Text = ""
            Me.txtUsuario.Focus()
            blnValue = False
            Return blnValue
        End If

        '/*validar contraseña*/
        If Me.UsuarioPass = "" Then
            MessageBox.Show("Ingrese el password del usuario", _
            Vichama.Configurations.Aplication.MessageBox.MensajeTitulo, _
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.txtpass.Text = ""
            Me.txtpass.Focus()
            blnValue = False
            Return blnValue
        End If

        Return blnValue

    End Function

    Private Sub AEntrar()

        If Validar() = False Then Exit Sub



        Try

            Dim obj As New Vichama.Business.Seguridad.Usuario
            Configuracion.Usuario = obj.Validar(Me.UsuarioNombre, Me.UsuarioPassEncrypt)


            If Configuracion.Usuario IsNot Nothing Then
                If Configuracion.Usuario.UsuarioId > 0 Then
                    Configuracion.Usuario.Conectado = True
                    Me.Close()
                End If

            Else
                Vichama.Configurations.Aplication.MessageBox.MensajeExclamation2( _
                "Usuario o contraseña incorrecta, por favor vuelva ha intentarlo")

                intIntentos += 1
                If intIntentos >= 3 Then
                    Me.Close()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


End Class